<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>库存管理-库存调配-修改库存调配</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">

	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a href="<%=basePath%>storageDistribute/selectByWhere.do">库存调配</a>
					<i class="icon-double-right"></i>
					<a>修改库存调配</a>
				</div>

				<div class="btn rightBtn">
					<input type="button" value="选择批次" onclick="toAddPurchaseRequestItem();" class="btn btn2"></input>
					<input type="button" value="修改" onclick="saveAddPurchaseRequest();" class="btn btn1"></input>
					<input type="button" value="返回" onclick="location.href='<%=basePath%>storageDistribute/selectByWhere.do'" class="btn btn2"></input>
				</div>
				<form id="generalStorageForm" class="form">
					<input type="hidden" readonly value="${storageDistribute.storageDistributeID }" class="form-control" name="storageDistributeID"></input>
					<div id="requisitionForm" class="box">
						<p>
							<label>调配单号</label>
							<input type="text" readonly value="${storageDistribute.distributeNumber }" class="form-control"></input>
							<label>调配人</label>

							<input type="text" readonly value="${userName }" class="form-control"></input>
							<input type="hidden" value="${userID }" name="commitPersonID"></input>
							<input type="hidden" value="${userID }" name="lastPesonID"></input>

							<label> 状态</label>
							<select name="status" class="form-control select">
								<option value="0" <c:if test="${storageDistribute.status=='0'}">selected</c:if>>未提交</option>
								<option value="1" <c:if test="${storageDistribute.status=='1'}">selected</c:if>>已提交</option>
							</select>

						</p>
						<p>
							<label> 原仓库</label>
							<select name="fromWarehouseID" onchange="gradeChange()" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }" <c:if test="${storageDistribute.fromWarehouseID==warehouse.warehousemanagementid }">selected</c:if>>${warehouse.warehousename }</option>
								</c:forEach>
							</select>

							<label> 目标仓库</label>
							<select name="toWarehouseID" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }" <c:if test="${storageDistribute.toWarehouseID==warehouse.warehousemanagementid }">selected</c:if>>${warehouse.warehousename }</option>
								</c:forEach>
							</select>

							<label>备注</label>
							<input type="text" name="remark" class="form-control doubleWidth" value="${storageDistribute.remark}"></input>

						</p>
					</div>
					<div class="tableBox box">
						<table class="table table-bordered table_text_center">
							<thead>
								<tr>
									<th>批次号</th>
									<th>物品名称</th>
									<th>物品条码</th>
									<th>品牌</th>

									<th>该批次数量</th>
									<th>调配数量</th>
									<th>规格</th>
									<th>调配数量小计</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="purchaseRequestItemTbody">
								<c:forEach items="${list2}" var="i" varStatus="in">
									<tr>
										<td>${i.batchNumber}</td>
										<td>${i.chineseName}</td>
										<td>${i.articleBarCode}</td>
										<td>${i.brand}</td>

										<td><input class='form-control' style='width: 60px;' name='gpcsumber' type='text' readonly value='${i.number}' /></td>
										<td><input class='form-control' style='width: 60px;' name='itemNumber2' maxlength='8' oninput='calculations(this)' type='text' value='${i.number1}' /></td>
										<td>
											<select class='form-control select' oninput='calculations2(this)'>
												<option value='${i.salesspecifications }'> ${i.salesspecifications } ${i.salesunit }</option>
												<option value='${i.purchaseSpecifications }'> ${i.purchaseSpecifications } ${i.salesunit }/${i.purchasingUnit }</option>
											</select>
										</td>
										<td><input class='form-control' style='width: 60px;' type='text' name='itemNumber' readonly value='${i.number1}' /></td>
										<td>
											<a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>
											<input type='hidden' name='itemIds' value='${i.id}' />
											<input type='hidden' name='intemInformationID' value='${i.intemInformationID}' /></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>

					</div>
					<div class="box updateBox ">
						<p>
							<label>创建人</label> 
							<input type="text" class="form-control doubleWidth" readonly value="${user2.loginName } "></input>
							<label>创建时间</label> 
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${storageDistribute.createTime  }' pattern="yyyy-MM-dd HH:mm:ss" /> "></input> 
							<br>
							<label>最后处理人</label> 
							<input type="text" class="form-control doubleWidth" readonly value="${user3.loginName } "></input>
							<label>最后处理时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${storageDistribute.lastTime  }' pattern="yyyy-MM-dd HH:mm:ss" /> "></input> 
							<br>
							<label>最后提交人</label> 
							<input type="text" class="form-control doubleWidth" readonly value="${user4.loginName } "></input>
							<label>最后提交时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${storageDistribute.commitTime  }' pattern="yyyy-MM-dd HH:mm:ss" />"></input>
						</p>
					</div>
				</form>
			</div>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
			<jsp:include page="/jsp/stock/storageDistribute/addPurchaseRequestItem.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
		<script src="<%=basePath%>My97DatePickers/WdatePicker.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			function gradeChange() {
				$('#purchaseRequestItemTbody').empty();
			}

			$(function() {
				// 任何需要执行的js特效 
			});

			function addToHasAddTable(item) {
				var html = "";

				html += "<tr>";
				html += "<td>" + item.batchNumber + "</td>";
				html += "<td>" + item.chineseName + "</td>";
				html += "<td>" + item.articleBarCode + "</td>";
				html += "<td>" + item.brand + "</td>";

				html += "<td><input class='form-control' style='width: 60px;' name='gpcsumber' type='text' readonly value='" + item.number + "'/></td>";
				html += "<td><input class='form-control' style='width: 60px;' name='itemNumber2' maxlength='8'  oninput='calculations(this)' type='text' value=''/></td>";

				html += "<td><select class='form-control select' oninput='calculations2(this)'>" +
					"<option value='" + item.salesSpecifications + "'>" + item.salesSpecifications + "" + item.salesUnit + "</option>" +
					"<option value='" + item.purchaseSpecifications + "'>" + item.purchaseSpecifications + "" + item.salesUnit + "/" + item.purchasingUnit + "</option>" +
					"</select></td>";
				html += "<td><input class='form-control' style='width: 60px;' type='text' name='itemNumber' readonly value=''/></td>";
				html += "<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>" +
					"<input type='hidden' name='itemIds' value='" + item.id + "'/>" +
					"<input type='hidden' name='intemInformationID' value='" + item.intemInformationID + "'/></td>";
				html += "</tr>";

				$("#purchaseRequestItemTbody").append(html);
			}
			/* 输入采购数量自动计算数量 */
			function calculations(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).val().replace(/\D/g, '')) * ($(obj).parent().next().find("option:selected").val()));
			}
			/* 选择规格下拉框在自动计算数量 */
			function calculations2(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).parent().parent().find("input[name='itemNumber2']").val().replace(/\D/g, '')) * ($(obj).find("option:selected").val()));
			}

			function deleteItem(obj) {
				$(obj).parent().parent().remove();
			}

			function validateForm() {
				/* 检查是否修改了物品 */
				var length = $("#purchaseRequestItemTbody [name='itemIds']").length;
				if(!length > 0) {
					Prompt.alert("提示", "物品种类数量必须大于0！")
					return false;
				}
				/* 检查数量是否大于0 */
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '' || num <= 0) {
						Prompt.alert("提示", "物品归还数量必须大于0！")
						return false;
					}
				}
				/* 检查目标仓库和原仓库是否相同 */
				var arrToWarehouseID = $("#generalStorageForm [name='fromWarehouseID'] option:selected").val();
				var arrFromWarehouseID = $("#generalStorageForm [name='toWarehouseID'] option:selected").val();
				if(arrToWarehouseID == arrFromWarehouseID) {
					Prompt.alert("提示", "（原仓库）和（目标仓库）不能相同")
					return false;
				}
				/* 检查归还小计数量是否大于领用出库数量 */
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				var pickingItemNumberArr = $("#purchaseRequestItemTbody [name='gpcsumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					var num2 = $(pickingItemNumberArr[i]).val();
					console.log(parseInt(num) > parseInt(num2));
					if(num.trim() == '' || num <= 0 ||
						num2.trim() == '' || num2 <= 0 || parseInt(num) > parseInt(num2)) {
						Prompt.alert("提示", "物品调配数量必须小于(领用出库数量-已归还数量)！")
						return false;
					}
				}
				return true;
			}

			function saveAddPurchaseRequest() {
				if(!validateForm()) return;
				$.ajax({
					url: "<%=basePath%>storageDistribute/update.do",
					type: "post",
					async: false,
					dataType: "json",
					data: $("#generalStorageForm").serialize(),
					success: function(data) {
						if(data.result == "success") {
							Prompt.alert("提示", "修改库存调配修改成功！", function() {
								location.href = "<%=basePath%>storageDistribute/selectByWhere.do";
							});
						} else {
							Prompt.alert("提示", "修改库存调配修改失败！");
						}
					},
					error: function() {
						Prompt.alert("提示", "修改库存调配修改失败！");
					}
				});
			}
		</script>
	</body>

</html>